package org.learn.leetcode;
//编写一个函数来查找字符串数组中的最长公共前缀。
//
//        如果不存在公共前缀，返回空字符串 ""
public class MaxPublicPrefix {
    public static void main(String[] args) {
        String[] arr = {"jack","jacke","jaisjk","jaieofl"};
        String[] arr1 = {"ab","a"};
//        System.out.println(longestCommonPrefix(arr));
        System.out.println(longestCommonPrefix(arr1));
    }

    //最长公共前坠
    public  static String longestCommonPrefix(String[] strs) {
        if(strs.length == 0){
            return "";
        }

        int rows = strs.length;//列
        int cols = strs[0].length();//行

        for (int i = 0; i < cols; i++) {
            char c = strs[0].charAt(i);
            for (int j = 1; j < rows; j++) {
                if(strs[j].length() == i || strs[j].charAt(i) != c){
                    return strs[0].substring(0,i);
                }
            }
        }
        return strs[0];
    }
}
